package com.lfg.leetcode.leetcode0;

public class No67addBinary {

    public static void main(String[] args) {
        String ret = addBinary("100", "110010");
        System.out.println(ret);
    }

    public static String addBinary(String a, String b) {
        StringBuilder ret = new StringBuilder();
        if (a.length() > b.length()) {
            String tmp = b;
            b = a;
            a = tmp;
        }
        int al = a.length();
        int bl = b.length();

        int sum = 0;
        int i = al - 1;
        int j = bl - 1;
        for (; i >= 0; i--) {
            sum += a.charAt(i) - '0' + b.charAt(j--) - '0';
            ret.insert(0, sum % 2);
            sum = sum / 2;
        }

        for (; j >= 0; j--) {
            sum += b.charAt(j) - '0';
            ret.insert(0, sum % 2);
            sum = sum / 2;
        }
        if (sum != 0) {
            ret.insert(0, 1);
        }
        return ret.toString();
    }
}

